//
//  WithdrawalViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/21.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "WithdrawalViewController.h"

@interface WithdrawalViewController ()
@property (weak, nonatomic) IBOutlet UITextField *withdrawalamount;//提现金额
@property (weak, nonatomic) IBOutlet UITextField *payPassword;//支付密码
@property (weak, nonatomic) IBOutlet UILabel *availableBalance;//可用余额

@end

@implementation WithdrawalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我要提现";
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    
    _availableBalance.text =  [[NSUserDefaults standardUserDefaults] objectForKey:@"Blance"];
   
}

- (IBAction)sureBtn:(id)sender {
    
    
    NSString *loginName = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginName"];
    NSDictionary *dict = @{
                           @"LoginName":loginName,
                           @"Dedit":self.withdrawalamount.text,
                           @"BankCode":@"中国工商银行"
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Money.Addwithdraw" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@", object);
        [self FengFu:[object[@"Data"] firstObject][@"WaterNumber"]];
    } withFailureBlock:^(NSError *error) {
        
    } progress:^(float progress) {
        
    }];
}


-(void)FengFu:(NSString *)requestId {
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSString *subledgerList = [NSString stringWithFormat:@"[{\"roleType\":\"0\",\"roleCode\":\"%@\",\"inOrOut\":\"1\",\"sum\":\"%@\",\"bizFlag\":\"0\"}]", loginId, self.withdrawalamount.text];
    NSDictionary *dic = @{
                          @"requestType":@"PFT0002",
                          @"requestId":requestId,
                          @"merchantCode":FengFuMerchantCode,
                          @"userIdIdentity":loginId,
                          @"sum":self.withdrawalamount.text,
                          @"payType":@"1",
                          @"subledgerList":subledgerList,
                          @"mainAccountType":@"",
                          @"mainAccountCode":@"",
                          @"passThrough":@"",
                          @"noticeUrl":@"www.baidu.com",
                          @"successReturnUrl":@"",
                          @"failReturnUrl":@"",
                          };
    NSString *sign = [[[[[[[[[[[[dic[@"requestType"] stringByAppendingString:dic[@"requestId"]] stringByAppendingString:dic[@"merchantCode"]] stringByAppendingString:dic[@"userIdIdentity"]] stringByAppendingString:dic[@"sum"]] stringByAppendingString:dic[@"payType"]] stringByAppendingString:dic[@"subledgerList"]] stringByAppendingString:dic[@"mainAccountType"]] stringByAppendingString:dic[@"mainAccountCode"]] stringByAppendingString:dic[@"passThrough"]] stringByAppendingString:dic[@"noticeUrl"]] stringByAppendingString:dic[@"successReturnUrl"]] stringByAppendingString:dic[@"failReturnUrl"]];
    NSString *signature = [HMACMD5 HMACMD5WithString:sign WithKey:FengFuKEY];
    NSDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dic];
    [dict setValue:signature forKey:@"signature"];
    [SumaPaySDK startService:dict callback:^(id resultDic, NSError *error) {
        NSLog(@"%@--------------%@", resultDic, error);
        NSDictionary *dic = [NetWorkManager dictionaryWithJsonString:resultDic];
        if ([dic[@"result"] integerValue] == 0) {
            if ([dic[@"msg"][@"result"] integerValue] == 00000) {
                [self confirmwithdraw:requestId and:dic[@"msg"]];
            }
        }
    }];
    
}

- (void)confirmwithdraw:(NSString *)requestId and:(NSDictionary *)dic{
    
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSDictionary *dict = @{
                           @"Loginid":loginId,
                           @"WaterNumber":requestId,
                           @"Dedit":self.withdrawalamount.text,
                           @"BankAccount":@"4417120101229115",
                           @"BankName":dic[@"bankName"],
                           @"Name":dic[@"name"],
                           @"noticeType":@"1",
                           };
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Money.confirmwithdraw" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@", object);
    } withFailureBlock:^(NSError *error) {
        
    } progress:^(float progress) {
        
    }];
}


@end
